using UnityEngine;

public class GlassAddonsSelector : MonoBehaviour
{
	public GameObjectsList addonsPrefabsList;

	public GameObject currentAddon;

	public int selectedAddonId;

	public void SetAddon(int id)
	{
		selectedAddonId = id;
		DestroyCurrentAddon();
		ClampSelectedAddon();
		CreateSelectedAddon();
	}

	private void CreateSelectedAddon()
	{
		GameObject instance = addonsPrefabsList.GetInstance(selectedAddonId);
		if (instance != null)
		{
			instance.transform.SetParent(base.transform);
			instance.transform.localPosition = Vector3.zero;
			instance.transform.localScale = Vector3.one;
			instance.transform.localRotation = Quaternion.identity;
			currentAddon = instance;
		}
	}

	private void ClampSelectedAddon()
	{
		if (selectedAddonId >= addonsPrefabsList.Count)
		{
			selectedAddonId = 0;
		}
		else if (selectedAddonId < 0)
		{
			selectedAddonId = addonsPrefabsList.Count - 1;
		}
	}

	private void DestroyCurrentAddon()
	{
		if (currentAddon != null)
		{
			UnityEngine.Object.Destroy(currentAddon);
			currentAddon = null;
		}
	}
}
